<template>
  <div>
    <div class="form">
      <div class="search">
        <el-form
          :model="queryParams"
          ref="queryForm"
          size="medium"
          :inline="true"
        >
          <el-form-item label="时间段" prop="status">
            <el-date-picker
              v-model="dateRange"
              type="daterange"
              range-separator="至"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
              value-format="yyyy-MM-dd"
            >
            </el-date-picker>
          </el-form-item>
        </el-form>
        <div>
          <el-button type="primary" @click="getList()" class="searchBtn"
            >搜索</el-button
          >
          <el-button @click="reset()" class="searchBtn">重置</el-button>
        </div>
      </div>
      <el-table
        class="listTable"
        v-loading="listLoading"
        :data="list"
        border
        fit
        highlight-current-row
        height="600"
      >
        <el-table-column label="序号" type="index" align="center">
        </el-table-column>
        <el-table-column label="巡检工单ID" prop="inspRecodeId" align="center">
        </el-table-column>
        <el-table-column label="巡检路线" prop="inspPlanName" align="center">
        </el-table-column>
        <el-table-column label="巡检时间" prop="inspRecodeTime" align="center">
        </el-table-column>
        <el-table-column label="巡检状态" prop="inspRecodeFlag" align="center">
          <template slot-scope="{ row }">
            <span>{{ getStatus(row.inspRecodeFlag) }}</span>
          </template>
        </el-table-column>
        <el-table-column label="执行方式" prop="inspPlanCycle" align="center">
          <template slot-scope="{ row }">
            <!-- {{row.inspPlanCycle}} -->
            <span>{{ getWay(row.inspPlanCycle) }}</span>
          </template>
        </el-table-column>
        <el-table-column
          label="操作"
          align="center"
          width="270"
          class-name="small-padding fixed-width"
        >
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              icon="el-icon-view"
              v-hasPermi="['financeProduct:product:remove']"
              style="color: green"
              @click.stop="open('detail', scope.row)"
              >查看</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        :total="total"
        layout="total, sizes, prev, pager, next"
        :page-size="limit"
        :page-sizes="[10, 20, 50, 100]"
        @size-change="handleSizeChange"
        :current-page.sync="curPage"
        @current-change="getList()"
        class="sortStyle"
      />
    </div>
    <el-dialog
      :visible.sync="visible"
      append-to-body
      :title="title"
      height="150px"
    >
      <el-form ref="form" label-suffix="：" :model="form" label-width="100px">
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="巡检工单ID" prop="inspRecodeId">
              <el-input
                disabled
                v-model="form.inspRecodeId"
                placeholder="请输入"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="巡检路线" prop="inspPlanName">
              <el-input
                disabled
                v-model="form.inspPlanName"
                placeholder="请输入"
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="执行方式" prop="inspPlanCycle">
              <el-select
                v-model="form.inspPlanCycle"
                disabled
                placeholder="请选择"
                style="width: 100%"
              >
                <el-option
                  v-for="dict in wayList"
                  :key="Number(dict.dictValue)"
                  :label="dict.dictLabel"
                  :value="Number(dict.dictValue)"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="巡检时间" prop="inspRecodeTime">
              <el-date-picker
                v-model="form.inspRecodeTime"
                type="datetime"
                disabled
                placeholder="请选择"
                style="width: 100%"
                value-format="yyyy-MM-dd HH:mm:ss"
              >
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <!-- <el-col :span="12">
            <el-form-item label="执行周期" prop="inspPlanCycle">
              <el-select
                v-model="form.inspPlanCycle"
                placeholder="请选择"
                style="width: 100%"
              >
                <el-option
                  v-for="item in cycleList"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col> -->
          <!-- <el-col :span="12">
            <el-form-item label="巡检标准" prop="devicePurchase">
              <el-select
                v-model="form.devicePurchase"
                placeholder="请选择"
                style="width: 100%"
              >
                <el-option
                  v-for="item in cycleList"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col> -->
        </el-row>
      </el-form>
      <div class="footer">
        <el-button @click="visible = false"> 取消 </el-button>
        <el-button type="primary" @click="save" v-if="form.inspRecodeFlag == 0">
          完成巡检
        </el-button>
        <!-- <div v-else>
          <el-button @click="visible = false"> 取消 </el-button>
          <el-button type="primary" @click="save"> 确认 </el-button>
        </div> -->
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { recodeList, recodeDetail, recodeFinish } from "@/api/system/device";
import {} from "@/api/article";
import { getToken } from "@/utils/auth";
import moment from "moment";
export default {
  components: {},
  filters: {
    source: function (value) {
      switch (value) {
        case "1":
          return "公众号";
        case "2":
          return "小程序";
      }
    },
  },
  data() {
    return {
      queryParams: {},
      form: {},
      cycleList: [
        { value: "1", label: "周" },
        { value: "2", label: "月" },
        { value: "3", label: "年" },
      ],
      visible: false,
      modelClass: null,
      classCard: false,
      list: [],
      listLoading: false,
      total: 0,
      limit: 10,
      curPage: 1,
      listData: [],
      studentName: "",
      // studentClass: '',
      phonenumber: "",
      work: "",
      page: this.$route.query.page,
      personClass: "",
      classList: [],
      userId: null,
      imageUrl: "",
      token: {
        Authorization: getToken(),
      },
      imageName: null,
      title: "",
      // 日期范围
      dateRange: [
        moment().startOf("month").format("yyyy-MM-DD"),
        moment().endOf("month").format("yyyy-MM-DD"),
      ],
      wayList: [
        // { value: "1", label: "单次" },
        // { value: "2", label: "周期" },
      ],
    };
  },
  created() {},
  mounted() {
    this.getDicts("exec_type").then((res) => {
      this.wayList = res.data;
    });
    this.getDicts("insp_status").then((res) => {
      this.statusList = res.data;
    });
    this.getList();
  },
  methods: {
    getWay(id) {
      let temp = "";
      this.wayList.forEach((e) => {
        if (e.dictValue == id) {
          temp = e.dictLabel;
        }
      });
      return temp;
    },
    getStatus(id) {
      let temp = "";
      this.statusList.forEach((e) => {
        if (e.dictValue == id) {
          temp = e.dictLabel;
        }
      });
      return temp;
    },
    handleAvatarSuccess(res, file) {
      this.imageName = res.imageName;
      this.imageUrl = URL.createObjectURL(file.raw);
    },
    save() {
      let params = {
        id: this.form.id,
      };
      recodeFinish(params).then((res) => {
        if (res.code == 200) {
          this.visible = false;
          this.$message.success(res.msg);
          this.getList();
        } else {
          this.$message.error(res.msg);
        }
      });
      // let params = {
      //   headPicture: this.imageName,
      //   name: this.form.name,
      //   teacherLevel: this.form.teacherLevel,
      //   introduction: this.form.introduction,
      // };
      // if (this.form.id) {
      //   params.id = this.form.id;
      //   editTeacher(params).then((res) => {
      //     this.$message.success(res.msg);
      //     this.getList();
      //   });
      // } else {
      //   addTeacher(params).then((res) => {
      //     this.$message.success(res.msg);
      //     this.getList();
      //   });
      // }
    },
    beforeAvatarUpload(file) {
      const isJPG = file.type === "image/jpeg";
      const isPNG = file.type === "image/png";
      const isLt2M = file.size / 1024 / 1024 < 20;
      if (!isJPG && !isPNG) {
        this.$message.error("上传头像图片只能是 JPG或PNG 格式!");
      }
      if (!isLt2M) {
        this.$message.error("上传头像图片大小不能超过 20MB!");
      }
      return (isJPG || isPNG) && isLt2M;
    },
    rowClick(row, column, cell) {
      this.modelClass = null;
      this.userId = row.userId;
      this.classCard = true;
    },
    getClassList() {
      this.classList = [{ classId: -1, className: "无班级人员" }];
      classList().then((res) => {
        if (res.code == "200") {
          res.rows.forEach((element) => {
            this.classList.push(element);
          });
        } else {
          this.$message({ message: res.msg, type: "error" });
        }
      });
    },
    del(id) {
      this.$confirm("确定删除该讲师吗？", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        teacherDel(id).then((res) => {
          if (res.code == "200") {
            this.$message({ message: res.msg, type: "success" });
            this.getList();
          } else {
            this.$message({ message: res.msg, type: "error" });
          }
        });
      });
    },
    exportList() {
      let params = {
        studentName: this.studentName,
        // className: this.studentClass,
        phonenumber: this.phonenumber,
        work: this.work,
      };
      this.$confirm("是否确认导出所有角色数据项?", "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(function () {
          return studentExport(params);
        })
        .then((response) => {
          this.download(response.msg);
        });
    },
    resetPsw(id) {
      this.$confirm("确定重置该学员密码吗？", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        let params = {
          userId: id,
          password: "123456",
        };
        resetPwd(params).then((res) => {
          if (res.code == "200") {
            this.$message({ message: res.msg, type: "success" });
          } else {
            this.$message({ message: res.msg, type: "error" });
          }
        });
      });
    },
    stop(id) {
      studentStop(id).then((res) => {
        if (res.code == 200) {
          this.$message({ message: res.msg, type: "success" });
          this.getList();
        } else {
          this.$message({ message: res.msg, type: "error" });
        }
      });
    },
    open(type, val) {
      this.visible = true;
      this.type = type;
      if (type === "add") {
        this.title = "新增";
        this.form = {
          inspPlanId: "XJJH-" + moment().format("YYYYMMDDHHmmss"),
        };
      } else if (type === "detail") {
        this.title = "详情";
        recodeDetail(val.id).then((res) => {
          this.form = res.data;
        });
      } else if (type === "edit") {
        this.title = "编辑";
        recodeDetail(val.id).then((res) => {
          this.form = res.data;
        });
      }
      // this.form = val;

      // this.title = "编辑";
      // this.imageUrl = "https://www.xlpx.org.cn/fire/image/" + val.headPicture;
    },
    reset() {
      // this.queryParams = {
      //   time: [
      //     moment().startOf("month").format("yyyy-MM-DD"),
      //     moment().endOf("month").format("yyyy-MM-DD"),
      //   ],
      // };
      this.curPage = 1;
      this.limit = 10;
      this.dateRange = [
        moment().startOf("month").format("yyyy-MM-DD"),
        moment().endOf("month").format("yyyy-MM-DD"),
      ];
      this.getList();
    },
    /** 查询用户列表 */
    getList() {
      let params = {
        pageNum: this.curPage,
        pageSize: this.limit,
      };
      this.listLoading = true;
      recodeList(this.addDateRange(params, this.dateRange)).then((res) => {
        this.listLoading = false;
        if (res.code == 200) {
          this.list = res.rows;
          // console.log('list', this.list);
          this.total = res.total;
        } else {
          this.$message({ message: res.msg, type: "error" });
        }
      });
    },
    preview(id) {
      this.$router.push({
        path: "/studentDetail",
        query: { id: id, page: this.curPage, personClass: this.personClass },
      });
    },

    handleDelete(id) {
      this.$confirm("是否确认删除？", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "info",
      })
        .then(() => {
          testDelete(id).then((res) => {
            if (res.code == "200") {
              this.getList();
              this.$message({ message: res.msg, type: "success" });
            } else {
              this.$message({ message: res.msg, type: "error" });
            }
          });
        })
        .catch(() => {});
    },
    handleSizeChange(val) {
      this.limit = val;
      this.getList(1);
    },
    add() {
      this.classCard = true;
      this.title = "讲师新增";
      this.form = {};
      this.imageName = null;
      this.imageUrl = null;
    },
    edit(val) {
      this.form = val;
      this.classCard = true;
      this.title = "讲师编辑";
      this.imageUrl = "https://www.xlpx.org.cn/fire/image/" + val.headPicture;
    },
  },
};
</script>
<style scoped>
.form {
  padding-left: 26px;
  padding-right: 24px;
  padding-top: 24px;
  background: #fff;
}
.search {
  display: flex;
  position: relative;
}
.search_info {
  width: 192px;
  height: 40px;
  margin-right: 16px;
}
.searchBtn {
  margin-left: 16px;
  height: 36px;
}
.sortStyle {
  height: 19px;
  float: right;
  /* right: 24px; */
  margin-top: 20px;
  margin-bottom: 30px;
}
.pos_right_btn {
  position: absolute;
  right: 0;
}
.opt_font {
  cursor: pointer;
  color: #2e82ff;
}
.detail_title {
  font-size: 16px;
  font-weight: 400;
  color: #212121;
  line-height: 22px;
}
.detail_title span {
  padding-left: 4px;
  background: #1e88e5;
  margin-right: 12px;
}
.row {
  display: flex;
  padding-bottom: 12px;
}
.detail_info {
  padding-top: 16px;
}
.info_left {
  display: flex;
}
.info_right {
  display: flex;
  padding-left: 202px;
}
.info_left_title {
  font-size: 14px;
  font-weight: 400;
  color: #616161;
  line-height: 20px;
  width: 112px;
  margin-right: 16px;
}
.info_left_content {
  font-size: 14px;
  font-weight: 400;
  color: #212121;
  line-height: 20px;
  width: 150px;
}
.pos_top {
  padding-top: 12px;
}
.detailTable {
  margin-top: 16px;
}
.opt_font {
  color: #1e88e5;
  cursor: pointer;
}
.page_pos {
  text-align: right;
  padding-top: 10px;
}
.backBtn {
  text-align: center;
  margin-top: 10px;
}
/deep/ .el-dialog {
  display: flex;
  flex-direction: column;
  margin: 0 !important;
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  /* max-height: calc(100% -30px); */
  /* max-width: ; */
}
/deep/ .el-dialog .el-dialog__body {
  flex: 1;
  height: 700px;
  overflow-y: auto;
  padding-top: 0;
}
.tab {
  display: flex;
  position: absolute;
  right: 32px;
  top: 14px;
}
.tab_border {
  border: 1px solid #409eff;
  color: #409eff;
  padding: 10px 12px;
  font-size: 14px;
  margin-right: 16px;
}
.change {
  border: 1px solid #f56c6c;
  color: #f56c6c;
}
.listTable {
  margin-top: 17px;
}
.out {
  position: absolute;
  right: 0;
}
.footer {
  text-align: center;
}
.point {
  cursor: pointer;
}
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
  border: 1px dashed #000;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>
